package cn.ibizlab.util.helper;

import cn.ibizlab.util.domain.ResponseWrapper;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class ResponseWrapperSerializer<T>  extends StdSerializer<ResponseWrapper<T>> {

  public ResponseWrapperSerializer(Class<ResponseWrapper<T>> clazz) {
    super(clazz);
  }

  public ResponseWrapperSerializer() {
    this(null);
  }

  @Override
  public void serialize(ResponseWrapper<T> responseWrapper, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    if (responseWrapper.isArray()) {
      jsonGenerator.writeObject(responseWrapper.getList());
    } else {
      jsonGenerator.writeObject(responseWrapper.getDto());
    }
  }

}